package engine;

import engine.data.City;

public final class CityAction {

	private static final byte MAKE_HAPPY_PEOPLE = 1;

	private static final byte MAKE_CONTENT_PEOPLE = 2;

	private static final byte CHANGE_PEOPLE_SPEC = 3;

	private static final byte SET_BUILDING = 4;

	private byte id;

	private City selectedCity;

	private byte selectedTileX;

	private byte selectedTileY;

	private Class selectedProductionType;

	private byte selectedProductionId;

	private byte selectedPeopleIndex;

	/**
	 * Constructs an action which changes production in a city.
	 * 
	 * @param city
	 *            the city
	 * @param productionType
	 *            the new production type (Unit.class or Building.Class)
	 * @param productionId
	 *            the new production id type
	 */
	public CityAction(final City city, final Class productionType, final byte productionId) {
		this.id = SET_BUILDING;
		this.selectedCity = city;
		this.selectedProductionType = productionType;
		this.selectedProductionId = productionId;
	}

	/**
	 * Constructs an action which changes speciality of artist/taxman/scientist people in a city.
	 * 
	 * @param city
	 *            the city
	 * @param peopleIndex
	 *            the index of changing speciality people
	 */
	public CityAction(final City city, final byte peopleIndex) {
		this.id = CHANGE_PEOPLE_SPEC;
		this.selectedCity = city;
		this.selectedPeopleIndex = peopleIndex;
	}

	/**
	 * Constructs an action which makes happy/content people from/on a city tile.
	 * 
	 * @param city
	 *            the city
	 * @param tileX
	 *            x coordinate of tile
	 * @param tileY
	 *            x coordinate of tile
	 * 
	 * This formula must be true : (-2<=tileX<=2 & -2<=tileY<=2 & tile!=cornerTile)
	 */
	public CityAction(final City city, final byte tileX, final byte tileY) {
		this.selectedCity = city;
		if (city.usedTiles[tileX][tileY])
			this.id = MAKE_HAPPY_PEOPLE;
		else
			this.id = MAKE_CONTENT_PEOPLE;
		this.selectedTileX = tileX;
		this.selectedTileY = tileY;
	}

	void executeAction() {
		switch (this.id) {
			case CityAction.MAKE_HAPPY_PEOPLE:
				this.selectedCity.makeHappyPeople(this.selectedTileX, this.selectedTileY);
				break;
			case CityAction.MAKE_CONTENT_PEOPLE:
				this.selectedCity.makeContentPeople(this.selectedTileX, this.selectedTileY);
				break;
			case CityAction.CHANGE_PEOPLE_SPEC:
				this.selectedCity.changePeopleSpec(this.selectedPeopleIndex);
				break;
			case CityAction.SET_BUILDING:
				this.selectedCity.setBuilding(selectedProductionType, selectedProductionId);
				break;
			default:
				break;
		}
	}

}
